package com.demo.service.impl;

import com.demo.common.Result;
import com.demo.mapper.StudentMapper;
import com.demo.pojo.Student;
import com.demo.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class StudentServiceImpl implements StudentService {
    private final StudentMapper studentMapper;


    /**
     * 查询单个学生
     *
     * @param studentId
     * @return
     */
    @Override
    public Result GetOne(Integer studentId) {
        Student student = studentMapper.getOne(studentId);
        if (student == null) {
            return Result.error("查不到该学生");
        }
        return Result.success(student);
    }

    /**
     * 获取所有学生信息
     *
     * @return
     */
    @Override
    public Result GetAll() {
        List<Student> students = studentMapper.getAll();
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getSex().equals("女")) {
                students.get(i).setSex("F");
            } else {
                students.get(i).setSex("M");
            }
        }
        return Result.success(students);
    }

    /**
     * 创建一个新学生
     * @return
     */
    @Override
    public Result NewAStudent(Student student) {
        Integer studentId = student.getStudentId();
        String name = student.getName();
        Integer age = student.getAge();
        String sex = student.getSex();
        System.out.println(student);
        if (!(studentId != null && String.valueOf(studentId).matches("\\d{10}"))) {
            return Result.error("学生Id必须为长度为10的数字");
        }
        if (!(name != null && name.length() >= 1 && name.length() <= 8)) {
            return Result.error("用户名不合法，用户名必须在1-8之间");
        }
        if (!(age != null && age > 0 && age <= 100)) {
            return Result.error("学生年龄必须在1-100之间");
        }
        if (!(sex != null && (sex.equals("女") || sex.equals("男")))) {
            return Result.error("学生性别错误");
        }
        Student search = studentMapper.getOne(studentId);
        if(search != null) {
            return Result.error("创建错误，该学号已存在");
        }
        if (sex.equals("女")) {
            sex = "F";
        } else {
            sex = "M";
        }
        try {
            studentMapper.createNew(studentId, name, age, sex);
        } catch (Exception e) {
            return Result.error("创建错误，该学号已存在");
        }
        return Result.success("创建成功");
    }

    /**
     * 删除单个学生
     * @param studentId
     * @return
     */
    @Override
    public Result deleteOne(Integer studentId) {
        Boolean End;
        try {
            End = studentMapper.deleteOne(studentId);
        } catch (Exception e) {
            return Result.error("删除失败，该学生学号对应学生不存在");
        }
        if (End != null && !End) {
            return Result.error("删除失败，该学生学号对应学生不存在");
        }

        return Result.success("删除成功");
    }

    /**
     * 修改单个学生数据
     * @param student
     * @return
     */
    @Override
    public Result editOne(Student student) {
        Integer studentId = student.getStudentId();
        String name = student.getName();
        Integer age = student.getAge();
        String sex = student.getSex();
        if (!(studentId != null && String.valueOf(studentId).matches("\\d{10}"))) {
            return Result.error("学生Id必须为长度为10的数字");
        }
        if (!(name != null && name.length() >= 1 && name.length() <= 8)) {
            return Result.error("用户名不合法，用户名必须在1-8之间");
        }
        if (!(age != null && age > 0 && age <= 100)) {
            return Result.error("学生年龄必须在1-100之间");
        }
        if (!(sex != null && (sex.equals("女") || sex.equals("男")))) {
            return Result.error("学生性别错误");
        }
        Student search = studentMapper.getOne(studentId);
        if(search == null) {
            return Result.error("修改错误，该学号对应学生不存在");
        }
        if (sex.equals("女")) {
            student.setSex("F");
        } else {
            student.setSex("M");
        }
        studentMapper.edit(student);
        return Result.error("修改成功");
    }
}
